package com.app.business;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.app.dto.ClothesDTO;
import com.utils.Constants;
import jxl.Cell;
import jxl.Sheet;

public class ReadSizeSheet {

	public Map<String, String> getSizeMap(Sheet sheet) {
		
		return getAllSize(sheet);
	}
	
	public Map<String, String> getAllSize(Sheet sheet){
		List<String> list = getAllItems(sheet);
		List<ClothesDTO> items = new ArrayList<ClothesDTO>();

		// 迭代集合查看每行的数据
		for (String ss : list) {
			ClothesDTO clothesDTO = new ClothesDTO();
			String[] sizeSplit = ss.split("###尺寸,");
			String sizeProStr;
			String commonProStr;
			if(sizeSplit.length>2){
				sizeProStr = sizeSplit[sizeSplit.length - 1];
				commonProStr = ss.replace("###尺寸,"+sizeProStr, "");
			}else{
				commonProStr = sizeSplit[0];
				sizeProStr = sizeSplit[1];
			}
			
			/**
			 * 基本属性
			 */
			String[] commonProSplitStr = commonProStr.split("###");
			/** 1 */
			String[] commonProSplit1 = commonProSplitStr[0].split(",");
			clothesDTO.setOuterId(commonProSplit1[1]);
			clothesDTO.setColorCode(commonProSplit1[3]);
			/** 2 */
			clothesDTO.setModelType(commonProSplitStr[1]);
			/** 3 */
			clothesDTO.setSize(commonProSplitStr[2]);
			Map<String, String> map1 = new LinkedHashMap<String, String>();
			for (int i = 1; i < commonProSplitStr.length; i++) {
				String tempStr = commonProSplitStr[i];
				if(tempStr.indexOf(",") == 0){
					tempStr = tempStr.substring(1,tempStr.length());
				}
				map1.put(tempStr.split(",")[0], tempStr);
			}
			clothesDTO.setCommonPro(map1);
			
			/**
			 * sku属性
			 */
			Map<String, String> map = new LinkedHashMap<String, String>();
			String[] sizeProSplitStr = sizeProStr.split("###");
			for (int i = 0; i < sizeProSplitStr.length; i++) {
				String tempStr = sizeProSplitStr[i];
				if(tempStr.indexOf(",") == 0){
					tempStr = tempStr.substring(1,tempStr.length());
				}
				map.put(tempStr.split(",")[0], tempStr);
			}
			clothesDTO.setMeasurement(map);
			
			items.add(clothesDTO);
		}
		
		return generateHtml(items);
	}
	
	public List<String> getAllItems(Sheet sheet){
		List<String> list = new ArrayList<String>();
		Cell cell = null;// 就是单个单元格
		StringBuffer oneType = new StringBuffer();
		// 开始循环，取得 cell 里的内容，这里都是按String来取的 为了省事，具体你自己可以按实际类型来取
		int size_true_data_row = Integer.parseInt(Constants.EXCEL_CUSTOM_DEFAULT.get("size"+"_true_data_row").toString()) - 1;
		for (int i = size_true_data_row; i < sheet.getRows(); i++) {
			StringBuffer sb = new StringBuffer();
			for (int j = 0; j < sheet.getColumns(); j++) {
				cell = sheet.getCell(j, i);
				sb.append(cell.getContents());
				sb.append(",");// 将单元格的每行内容用逗号隔开
			}
			if(sb.toString().replaceAll(",", "").trim().length() < 1){
				if(oneType.toString().trim().length() > 1){
					list.add(oneType.toString());
				}
				oneType = null;
				oneType = new StringBuffer();
			}else{
				oneType.append(sb.toString() + Constants.SPLIT_SIGN);// 将每行的字符串用一个String类型的集合保存。
			}
			
			if( i == sheet.getRows()-1 && oneType.toString().trim().length() > 1){
				list.add(oneType.toString());
				oneType = new StringBuffer();
			}
		}
		
		return list;
	}

	public Map<String, String> generateHtml(List<ClothesDTO> items){
		Map<String, String> sizeMap = new HashMap<String, String>();
		StringBuffer html = new StringBuffer();
		for (ClothesDTO clothesDTO : items) {
			html.append("<table id='myappItemSizeDescByZxf2012' style='font-size: 12.0px;display:myappItemSizeDescByZxf2012' border='1' cellspacing='0' cellpadding='0' width='720' bgcolor='#777777' align='center'>");
		
			html.append("<tbody>");
		
			/** -----遍历所有公共属性 ---------- */
			Set<Entry<String, String>> commonPro = clothesDTO.getCommonPro().entrySet();
			int firstRow = 0;
			String bgcolor = "#ffffff";
			boolean colorFlag = true;
			for (Entry<String, String> entry : commonPro) {
				String[] split = entry.getValue().replace(entry.getKey()+",,", entry.getKey()+",").split(",");
				if(firstRow == 0){
					html.append("<tr style='color: #ffffff;' height='18' bgcolor='#dddddd'>");
					for (int i = 0; i < split.length; i++) {
						html.append("<td style='font-size: 14.0px;' bgcolor='#555555' height='26' valign='center' align='middle'><p>"+split[i]+"</p></td>");
					}
					html.append("</tr>");
				}else{
					if(colorFlag){
						bgcolor = "#ffffff";
						colorFlag = false;
					}else{
						bgcolor = "#efefef";
						colorFlag = true;
					}
					html.append("<tr height='18'>");
					for (int i = 0; i < split.length; i++) {
						html.append("<td bgcolor='"+bgcolor+"' height='26' valign='center' align='middle'><p>"+split[i]+"</p></td>");
					}
					html.append("</tr>");
				}
				firstRow = -1;
			}
			//====================================================================
			
			/** -----begin遍历所有属性 ---------- */
			Set<Entry<String, String>> measurement = clothesDTO.getMeasurement().entrySet();
			for (Entry<String, String> entry : measurement) {
				if(colorFlag){
					bgcolor = "#ffffff";
					colorFlag = false;
				}else{
					bgcolor = "#efefef";
					colorFlag = true;
				}
				String[] split = entry.getValue().split(",");
				html.append("<tr height='18'>");
				for (int i = 0; i < split.length; i++) {
					html.append("<td bgcolor='"+bgcolor+"' height='26' valign='center' align='middle'><p>"+split[i]+"</p></td>");
				}
				html.append("</tr>");
			}
			/** -----begin遍历所有属性 ---------- */
			html.append("</tbody>");
			html.append("</table>");
			
			sizeMap.put(clothesDTO.getOuterId(), html.toString());
			html.delete(0, html.length());
		}
		
		return sizeMap;
	}

}
